const db = wx.cloud.database()
Page({
  data: {
    name: "",
    list: [], //热门搜索列表
    reSearch: [], //近期搜索
  },
  onLoad() {
    this.getBestMenuList();
    let reSearch = wx.getStorageSync('reSearch')
    this.setData({
      reSearch
    })
  },
  getBestMenuList() {
    db.collection('menu')
      .orderBy('views', 'desc')
      .limit(9)
      .get()
      .then(res => {
        this.setData({
          list: res.data
        })
      })
      .catch(err => {
        console.log(err);
      })
  },
  myInput(e) {
    this.setData({
      name: e.detail.value
    })
  },
  async mySearch() {
    if (this.data.name != "") {
      let reSearch = wx.getStorageSync('reSearch') || [];
      if (reSearch.length >= 10) {
        reSearch.pop()
      }
      let name = this.data.name;
      reSearch.forEach((item, index) => {
        if (item == name) {
          reSearch.splice(index, 1)
        }
      })
      reSearch.unshift(name)
      wx.setStorageSync('reSearch', reSearch)
      this.setData({
        reSearch
      })
      wx.navigateTo({
        url: '/pages/list/list?name=' + name,
      })
    }
  },
  hotWord(e) {
    this.setData({
      name: e.currentTarget.id
    })
  }
})